﻿using BCCommon;
using BCCommon.Enums;
using BCData.Trading.SaleContractReceiptRecord;
using BCDto.Trading.SaleContractReceiptRecord;
using BCEntity.Trading.SaleContractReceiptRecord;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.Trading.SaleContractReceiptRecord
{
    public class SaleContractReceiptRecordService : ISaleContractReceiptRecordService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IApplicationContextService applicationContextService;
        private readonly IRedisService redisService;
        private readonly ISaleContractReceiptRecordData saleContractReceiptRecordData;
        private readonly string key = string.Format("{0}", EntityNameConst.SaleContractReceiptRecordEntity);

        public SaleContractReceiptRecordService(IDatabaseContext databaseContext,
            IApplicationContextService applicationContextService,
            IRedisService redisService,
            ISaleContractReceiptRecordData saleContractReceiptRecordData)
        {
            this.databaseContext = databaseContext;
            this.applicationContextService = applicationContextService;
            this.redisService = redisService;
            this.saleContractReceiptRecordData = saleContractReceiptRecordData;
        }

        #region 检查销售合同收款记录是否存在
        public bool Exists(long saleContractReceiptRecordId)
        {
            return this.saleContractReceiptRecordData.Exists(saleContractReceiptRecordId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加销售合同收款记录
        public SaleContractReceiptRecordDto Add(SaleContractReceiptRecordRequestDto requestDto)
        {
            var entity = requestDto.As<SaleContractReceiptRecordEntity>();
            return this.saleContractReceiptRecordData.Add(entity).GetAwaiter().GetResult().As<SaleContractReceiptRecordDto>();
        }
        #endregion

        #region 修改销售合同收款记录
        public SaleContractReceiptRecordDto Update(SaleContractReceiptRecordPutDto putDto)
        {
            if (!this.Exists(putDto.SaleContractReceiptRecordId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<SaleContractReceiptRecordEntity>();
            var result = this.saleContractReceiptRecordData.Update(entity).GetAwaiter().GetResult().As<SaleContractReceiptRecordDto>();
            string cacheKey = this.redisService.GetKey(key, entity.SaleContractReceiptRecordId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除销售合同收款记录
        public bool Delete(long saleContractReceiptRecordId)
        {
            if (!this.Exists(saleContractReceiptRecordId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.saleContractReceiptRecordData.Delete(saleContractReceiptRecordId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, saleContractReceiptRecordId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取销售合同收款记录
        public SaleContractReceiptRecordDto Get(long saleContractReceiptRecordId)
        {
            string cacheKey = this.redisService.GetKey(key, saleContractReceiptRecordId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.saleContractReceiptRecordData.GetEntity(saleContractReceiptRecordId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<SaleContractReceiptRecordDto>();
        }
        #endregion

        #region 获取销售合同收款记录列表
        public IEnumerable<SaleContractReceiptRecordDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.saleContractReceiptRecordData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<SaleContractReceiptRecordDto>>();
        }
        #endregion

        #region 获取销售合同收款记录分页
        public PageableList<SaleContractReceiptRecordDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.saleContractReceiptRecordData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<SaleContractReceiptRecordDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<SaleContractReceiptRecordDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}